import Vue from 'vue'
import VueRouter from 'vue-router'
import Questions from '@/views/questions/index.vue'
import Find from '@/views/find/index.vue'
import Company from '@/views/company/index.vue'
import Login from '@/views/login/login.vue'
import MyChart from '@/views/login/MyChart.vue'
import TechnicList from '@/views/find/technicList.vue'
import Chart from '@/views/find/chart.vue'
import PlayQuestions from '@/views/questions/components/playQuestions.vue'
import My from '@/views/my/index.vue'
import MyTabbar from '@/components/MyTabbar.vue'
import AnswerCard from '@/views/questions/answerCard.vue'
import Share from '@/views/find/share.vue'
import ShareDetail from '@/views/find/sharedetail.vue'
import FindDetail from '@/views/find/finddetail.vue'
import SearchShare from '@/views/find/searchshare.vue'
import Tach from '@/views/find/components/tach.vue'
import Info from '@/views/my/edit/info.vue'
import Message from '@/views/my/mymessage.vue'
import CompanyDetail from '@/views/companyDetail/index.vue'
import { getLocal } from '@/utils/auth.js' // 导入获取token的方法
import CompanyPosition from '@/views/companyDetail/components/companyPosition.vue'

import Vant from 'vant'
import 'vant/lib/index.css'

Vue.use(Vant)
Vue.use(VueRouter)

const routes = [
  {
    path: '/questions',
    name: 'Questions',
    components: { default: Questions, bottom: MyTabbar },
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/find',
    name: 'Find',
    components: { default: Find, bottom: MyTabbar }
  },
  {
    path: '/',
    name: 'Company',
    components: { default: Company, bottom: MyTabbar },
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/companyDetail/:id',
    name: 'CompanyDetail',
    components: { default: CompanyDetail }
  },
  {
    path: '/companyPosition',
    name: 'CompanyPosition',
    components: { default: CompanyPosition }
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/mychart',
    name: 'MyChart',
    component: MyChart
  },
  {
    path: '/playQuestions',
    name: 'PlayQuestions',
    component: PlayQuestions
  },
  {
    path: '/chart',
    name: 'Chart',
    component: Chart
  },
  {
    path: '/technicList',
    name: 'TechnicList',
    component: TechnicList
  },
  {
    path: '/my',
    name: 'My',
    components: { default: My, bottom: MyTabbar }
  },
  {
    path: '/info',
    name: 'Info',
    component: Info
  },
  {
    path: '/my/message',
    name: 'Message',
    component: Message
  },
  {
    path: '/answerCard',
    name: 'AnswerCard',
    component: AnswerCard
  },
  {
    path: '/share',
    name: 'Share',
    component: Share
  },
  {
    path: '/Sharedetail',
    name: 'ShareDetail',
    component: ShareDetail
  },

  {
    path: '/finddetail',
    name: 'FindDetail',
    component: FindDetail
  },
  {
    path: '/questions/playQuestions',
    name: 'PlayQuestions',
    component: PlayQuestions
  },
  {
    path: '/searchshare',
    name: 'SearchShare',
    component: SearchShare
  },
  {
    path: '/tach',
    name: 'Tach',
    component: Tach
  }
]

const router = new VueRouter({
  routes
})

// 导航守卫
// 本项目使用路由元模式,如果页面需要登录才能访问,需要在对应路由添加 needLogin:true
/*
   一.有token:
      1.如果是想要进入登陆页,则跳转到首页
      2.如果是想要进入其它页面,则正常跳转

   二.没有token
      1.如果是想要进入需要登录才能访问的页面,则跳转到登录页
      2.如果是想要进入不用登录就能访问的页面,则正常跳转
*/

router.beforeEach((to, from, next) => {
  if (getLocal('userToken')) {
    // 有token的情况
    if (to.path.toLowerCase() === '/login') {
      next('/')
    } else {
      next()
    }
  } else {
    // 没有token的情况
    if (to.meta.needLogin) {
      // 想要进入需要登录才能访问的页面
      // 把来源的路由定义成参数'redirect'传到login页面
      next('/login?redirect=' + to.fullPath)
    } else {
      // 想要进入不需要登录就能访问的页面
      next()
    }
  }
})

// 低版本路由bug处理
const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return routerPush.call(this, location).catch(error => error)
}

export default router
